package com.demo.studentmanager.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.studentmanager.dto.ClassAddDto;
import com.demo.studentmanager.dto.ClassListDto;
import com.demo.studentmanager.dto.ClassUpdDto;
import com.demo.studentmanager.entity.StudentClass;
import com.demo.studentmanager.result.ResultCode;
import com.demo.studentmanager.service.IClassService;
import com.demo.studentmanager.service.IDepartmentService;
import com.demo.studentmanager.service.ITeacherService;
import com.demo.studentmanager.util.CheckEntityUtil;
import com.demo.studentmanager.vo.ClassVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;


/**
 * @author zjia
 */
@RestController
@Api(tags = "班级模块")
@RequestMapping("/class")
public class ClassController {

    @Autowired
    IClassService classService;

    @Autowired
    IDepartmentService departmentService;

    @Autowired
    ITeacherService teacherService;

    @RequiresRoles(value={"teacher","admin"},logical= Logical.OR)
    @GetMapping
    @ApiOperation(value = "查询班级信息")
    public IPage<ClassVo> getClassList(@Validated ClassListDto classListDto) {
        return classService.getClassList(classListDto);
    }


    @RequiresRoles("admin")
    @PostMapping
    @ApiOperation(value = "增加班级信息")
    public void addClass(@Validated ClassAddDto classAddDto) {

        CheckEntityUtil.isNotNullById(classAddDto.getClassno(), classService, ResultCode.CLASS_ACCOUNT_EXIST);

        CheckEntityUtil.isNullById(classAddDto.getDeptno(), departmentService, ResultCode.DEPARTMENT_NOT_EXIST);

        CheckEntityUtil.isNullById(classAddDto.getTno(), teacherService, ResultCode.TEACHER_ACCOUNT_NOT_EXIST);

        StudentClass studentClass = new StudentClass();
        classAddDto.convertToEntity(studentClass);

        classService.save(studentClass);
    }


    @RequiresRoles("admin")
    @PutMapping
    @ApiOperation(value = "修改班级信息")
    public void upClass(@Validated ClassUpdDto classUpdDto) {

        StudentClass studentClass = CheckEntityUtil.isNullById(classUpdDto.getClassno(), classService, ResultCode.CLASS_ACCOUNT_NOT_EXIST);

        if (classUpdDto.getDeptno() != null) {
            CheckEntityUtil.isNullById(classUpdDto.getDeptno(), departmentService, ResultCode.DEPARTMENT_NOT_EXIST);
        }

        if (classUpdDto.getTno() != null) {
            CheckEntityUtil.isNullById(classUpdDto.getTno(), teacherService, ResultCode.TEACHER_ACCOUNT_NOT_EXIST);
        }

        classUpdDto.convertToEntity(studentClass);

        classService.updateById(studentClass);

    }


    @RequiresRoles("admin")
    @DeleteMapping("/{classno}")
    @ApiOperation(value = "删除班级信息")
    public void delClass(@NotNull @PathVariable String classno) {

        CheckEntityUtil.isNullById(classno, classService, ResultCode.CLASS_ACCOUNT_NOT_EXIST);

        classService.removeById(classno);

    }


}
